package org.batatasframework.strategy;

import javax.servlet.http.HttpServletRequest;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

import cn.bonoon.kernel.security.LogonUser;

public class ServerStrategyEvent {
	
	private ServerStrategyResult result;
	private LogonUser logon;
	private Boolean hasLogon;
	private boolean runable = true;
	private final HttpServletRequest request;
	
	public ServerStrategyEvent(HttpServletRequest request){
		this.request = request;
	}
	
	public boolean hasLogon(){
		internalResolve();
		return hasLogon;
	}
	
	private void internalResolve(){
		if(null == hasLogon){
			Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
	        if(null != authentication){
	        	Object p = authentication.getPrincipal();
	        	if(p instanceof LogonUser){
	        		logon = (LogonUser)p;
	        	}
	        }

    		hasLogon = null != logon;
		}
	}
	
	public LogonUser getLogon() {
		internalResolve();
		return logon;
	}
	
	public boolean isRunable() {
		return runable;
	}
	
	public void setResult(ServerStrategyResult result) {
		this.result = result;
	}
	
	public void setRunable(boolean runable) {
		this.runable = runable;
	}
	
	public ServerStrategyResult getResult() {
		return result;
	}

	public HttpServletRequest getRequest() {
		return request;
	}
}
